import { Request, Response } from 'express'
import { AIModelModel } from '../models/aiModel.model'
import { ApiResponse } from '../types'

export class AIModelController {
  // 获取用户的所有模型
  async getModels(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      if (!userId) {
        return res.status(401).json({
          status: 'error',
          message: '未授权'
        } as ApiResponse)
      }

      const models = await AIModelModel.findByUserId(userId)
      res.json({
        status: 'success',
        data: models
      } as ApiResponse)
    } catch (error) {
      console.error('Get models error:', error)
      res.status(500).json({
        status: 'error',
        message: '服务器错误'
      } as ApiResponse)
    }
  }

  // 创建模型
  async createModel(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      if (!userId) {
        return res.status(401).json({
          status: 'error',
          message: '未授权'
        } as ApiResponse)
      }

      const { name, model, apiKey, baseUrl, isDefault } = req.body

      if (!name || !model || !apiKey || !baseUrl) {
        return res.status(400).json({
          status: 'error',
          message: '缺少必要参数'
        } as ApiResponse)
      }

      const id = await AIModelModel.create({
        user_id: userId,
        name,
        model,
        api_key: apiKey,
        base_url: baseUrl,
        is_default: isDefault || false
      })

      res.status(201).json({
        status: 'success',
        data: { id }
      } as ApiResponse)
    } catch (error) {
      console.error('Create model error:', error)
      res.status(500).json({
        status: 'error',
        message: '服务器错误'
      } as ApiResponse)
    }
  }

  // 更新模型
  async updateModel(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      if (!userId) {
        return res.status(401).json({
          status: 'error',
          message: '未授权'
        } as ApiResponse)
      }

      const { id } = req.params
      const { name, model, apiKey, baseUrl, isDefault } = req.body

      const success = await AIModelModel.update(Number(id), userId, {
        name,
        model,
        api_key: apiKey,
        base_url: baseUrl,
        is_default: isDefault
      })

      if (!success) {
        return res.status(404).json({
          status: 'error',
          message: '模型不存在或无权限修改'
        } as ApiResponse)
      }

      res.json({
        status: 'success',
        message: '更新成功'
      } as ApiResponse)
    } catch (error) {
      console.error('Update model error:', error)
      res.status(500).json({
        status: 'error',
        message: '服务器错误'
      } as ApiResponse)
    }
  }

  // 删除模型
  async deleteModel(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      if (!userId) {
        return res.status(401).json({
          status: 'error',
          message: '未授权'
        } as ApiResponse)
      }

      const { id } = req.params
      const success = await AIModelModel.delete(Number(id), userId)

      if (!success) {
        return res.status(404).json({
          status: 'error',
          message: '模型不存在、无权限删除或不能删除默认模型'
        } as ApiResponse)
      }

      res.json({
        status: 'success',
        message: '删除成功'
      } as ApiResponse)
    } catch (error) {
      console.error('Delete model error:', error)
      res.status(500).json({
        status: 'error',
        message: '服务器错误'
      } as ApiResponse)
    }
  }

  // 设置默认模型
  async setDefault(req: Request, res: Response) {
    try {
      const userId = req.user?.id
      if (!userId) {
        return res.status(401).json({
          status: 'error',
          message: '未授权'
        } as ApiResponse)
      }

      const { id } = req.params
      const success = await AIModelModel.setDefault(Number(id), userId)

      if (!success) {
        return res.status(404).json({
          status: 'error',
          message: '模型不存在或无权限设置'
        } as ApiResponse)
      }

      res.json({
        status: 'success',
        message: '设置成功'
      } as ApiResponse)
    } catch (error) {
      console.error('Set default model error:', error)
      res.status(500).json({
        status: 'error',
        message: '服务器错误'
      } as ApiResponse)
    }
  }
} 